#include <asm/segment.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>

char psinfo_buffer[1024];

int sprintf(char *buf, const char *fmt, ...)
{
	va_list args; int i;
	va_start(args, fmt);
	i=vsprintf(buf, fmt, args);
	va_end(args);
	return i;
}

int psinfo_read(int dev,char * buf,int count,off_t * f_pos){
	struct task_struct *p;	
	int k,i,j,n;

	for(j=0;j<1024;j++)
		psinfo_buffer[j]='\0';
	n=0;
	if(dev==0){
		n+=sprintf(&psinfo_buffer[n],"pid\tstate\tfather\tcounter\tstart_time\n");
		for(i=0;i<64;i++){
			p=task[i];
			if(p!=NULL){
				n+=sprintf(&psinfo_buffer[n],"%d\t%d\t%d\t%d\t%d\n",p->pid,p->state,p->father,p->counter,p->start_time);
			}		
		}	
	}

	for(k=0;k<count;k++){
		if(psinfo_buffer[k+(*f_pos)]=='\0')
			break;
		put_fs_byte(psinfo_buffer[k+(*f_pos)],buf+k+(*f_pos));		
	}	
	(*f_pos)=(*f_pos)+k;
	return k;
}

int hdinfo_read(int dev,char * buf,int count,off_t * f_pos){
	struct super_block * sb; 
	int n,k,j;

	sb=get_super(dev);

	for(j=0;j<1024;j++)
		psinfo_buffer[j]='\0';
	n=0;
	n+=sprintf(&psinfo_buffer[n],"total_blocks:%d;\n",sb->s_nzones);
	n+=sprintf(&psinfo_buffer[n],"free_blocks:%d;\n",sb->s_nzones-sb->s_zmap_blocks);
	n+=sprintf(&psinfo_buffer[n],"used_blocks:%d;\n",sb->s_zmap_blocks);
	n+=sprintf(&psinfo_buffer[n],"total_inodes:%d;\n",sb->s_ninodes);

	for(k=0;k<count;k++){
		if(psinfo_buffer[k+(*f_pos)]=='\0')
			break;
		put_fs_byte(psinfo_buffer[k+(*f_pos)],buf+k+(*f_pos));		
	}	
	(*f_pos)=(*f_pos)+k;
	return k;
}

int inodeinfo_read(int dev,char * buf,int count,off_t * f_pos){
	struct m_inode * inode;
	int n,k,j;

	for(j=0;j<1024;j++)
		psinfo_buffer[j]='\0';
	n=0;
	for(j=1;j<6;j++){
		inode = iget(dev,j);
		n+=sprintf(&psinfo_buffer[n],"inr:%d;zone[0]:%d\n",j,inode->i_zone[0]);
	}

	for(k=0;k<count;k++){
		if(psinfo_buffer[k+(*f_pos)]=='\0')
			break;
		put_fs_byte(psinfo_buffer[k+(*f_pos)],buf+k+(*f_pos));		
	}	
	(*f_pos)=(*f_pos)+k;
	return k;
}
